package com.reef.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.reef.dto.LendBookDto;
import com.reef.entity.Lend_book;
import com.reef.entity.Result;
import com.reef.service.LendBookService;
@RestController
public class Lend_bookController {
@Autowired
private LendBookService lbService;
@RequestMapping("/insertLendbook")
public Object insertLendbook(Lend_book lendbook) {
	Lend_book lb = lbService.insertLendbook(lendbook);
	if(lb!=null) {
		Result result = new Result("添加成功","",true);
		return result;
	}else {
		Result result = new Result("添加失败","",false);
		return result;
	}
}
@RequestMapping("/updateByBackBookDate")
public Object updateByBackBookDate(String realdate, int isback, int sernum) {
	int lb = lbService.updateByBackBookDate(realdate, isback, sernum);
	if(lb!=0) {
		Result result = new Result("修改成功","",true);
		return result;
	}else {
		Result result = new Result("修改失败","",false);
		return result;
	}
}
/**
 * 多条件动态查询
 */
@RequestMapping("/findByBooknameAndReaName")
public Object findByBooknameAndReaName(LendBookDto lbDto,int page,int limit) {
	//getTotalElements()指实体中有多少元素
	//分页查询 springBoot2.0以下版本用new 实例化，2.0以上版本使用的是PageRequest
	Pageable pageable = PageRequest.of(page-1, limit);
	Page<Lend_book> pagelist  = lbService.findByBooknameAndReaName(lbDto, pageable);
	Map<String, Object> map =new HashMap<String, Object>();
	map.put("code", 0);
	map.put("msg", "");
	map.put("count", pagelist.getTotalElements());
	map.put("data", pagelist.getContent());
	return map;
}
}